
package com.be.beadmin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.util.StringUtils;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.service.dto.input.FactoryCalendarItemDto;
import com.be.beadmin.service.dto.output.FactoryCalendarItemEx;
import com.be.beadmin.service.entity.FactoryCalendar;
import com.be.beadmin.service.entity.FactoryCalendarItem;
import com.be.beadmin.service.entity.FactoryCalendarItem_;
import com.be.beadmin.service.mapper.FactoryCalendarItemMapper;
import com.be.beadmin.service.mapper.FactoryCalendarItemMapperEx;
import com.be.beadmin.service.repository.FactoryCalendarItemRepository;
import com.be.beadmin.service.repository.FactoryCalendarRepository;
import com.be.beadmin.service.service.FactoryCalendarItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @description 服务实现
* @author chenwei
* @date 2023-02-22
**/
@Service
@RequiredArgsConstructor
public class FactoryCalendarItemServiceImpl implements FactoryCalendarItemService {

    @Autowired
    private  FactoryCalendarItemRepository factoryCalendarItemRepository;

    @Autowired
    private FactoryCalendarRepository factoryCalendarRepository;

    @Autowired
    private  FactoryCalendarItemMapper factoryCalendarItemMapper;
    @Autowired
    private  FactoryCalendarItemMapperEx factoryCalendarItemExMapper;

    @Override
    public Page<FactoryCalendarItemEx> getFactoryCalendarItemByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<FactoryCalendarItem> page = factoryCalendarItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(FactoryCalendarItem_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(FactoryCalendarItem_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<FactoryCalendarItemEx> page1 = page.map(factoryCalendarItemExMapper::toEntity);
            return page1;

    }

    @Override
    public List<FactoryCalendarItemEx> addFactoryCalendarItem(FactoryCalendarItemDto factoryCalendarItemDto) {
        FactoryCalendar factoryCalendar = factoryCalendarRepository.findFactoryCalendarById(factoryCalendarItemDto.getFactoryCalendarId());
        if(ObjectUtil.isEmpty(factoryCalendar)) {
            throw new BeadminException("所对应的主日历不存在请重新查询");
        }
        FactoryCalendarItem factoryCalendarItem = factoryCalendarItemMapper.toEntity(factoryCalendarItemDto);
        factoryCalendarItem.setFactoryCalendar(factoryCalendar);
        factoryCalendarItemRepository.save(factoryCalendarItem);

        List<FactoryCalendarItemEx> factoryCalendarItemExes = findDays(factoryCalendarItemDto.getFactoryCalendarId(),factoryCalendarItemDto.getDate());

        return factoryCalendarItemExes;


    }

    @Override
    public void updateFactoryCalendarItem(FactoryCalendarItemDto factoryCalendarItemDto) {
        FactoryCalendarItem result = factoryCalendarItemRepository.findFactoryCalendarItemByIdAndDelete(factoryCalendarItemDto.getId(), 0);
        if(ObjectUtil.isNotEmpty(result)) {
            throw new BeadminException("FactoryCalendarItem不存在,请重新查询");
        }
        FactoryCalendarItem factoryCalendarItem = factoryCalendarItemMapper.toEntity(factoryCalendarItemDto);
        factoryCalendarItemRepository.save(factoryCalendarItem);
    }


    @Override
    public void delFactoryCalendarItem(String id) {
        FactoryCalendarItem factoryCalendarItem = factoryCalendarItemRepository.findFactoryCalendarItemById(id);
        factoryCalendarItem.setDelete(1);
        factoryCalendarItemRepository.save(factoryCalendarItem);
    }

    @Override
    public void delFactoryCalendarItems (List<String> ids) { }

    @Override
    public List<FactoryCalendarItemEx> findDays(String factoryCalendarId, Date date) {


        Calendar preCalendar = Calendar.getInstance(); //
        preCalendar.setTime(date);
        preCalendar.add(Calendar.DAY_OF_MONTH, -50);


        Calendar afterCalendar = Calendar.getInstance(); //
        afterCalendar.setTime(date);
        afterCalendar.add(Calendar.DAY_OF_MONTH, 50);

        Date preDate = preCalendar.getTime();
        Date afterDate = afterCalendar.getTime();
        List<FactoryCalendarItem> page = factoryCalendarItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 = criteriaBuilder.equal(root.get(FactoryCalendarItem_.delete), 0); //过滤为0
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.greaterThanOrEqualTo(root.get(FactoryCalendarItem_.DATE), preDate); //过滤为0
                    list.add(pre2);
                    Predicate pre3 = criteriaBuilder.lessThanOrEqualTo(root.get(FactoryCalendarItem_.DATE), afterDate); //过滤为0
                    list.add(pre3);

                    if (!StringUtils.isEmpty(factoryCalendarId)) {
                        Predicate pre4 = criteriaBuilder.equal(root.get(FactoryCalendarItem_.FACTORY_CALENDAR)
                                .get("id"), factoryCalendarId); //过滤为0
                        list.add(pre4);
                    }

                    Predicate[] p = new Predicate[list.size()];
                    return  criteriaBuilder.and(list.toArray(p));
                }
                );
        List<FactoryCalendarItemEx> page1 = page.stream().map(factoryCalendarItemExMapper::toEntity).collect(Collectors.toList());
        return page1;

    }
}